<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { ChallengeApi } from '@/api/controller';
  import { Toastloading, ToastWarning, ToastSuccess } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import updateJackpot from './updateJackpot.vue';
  const $emit = defineEmits(['updateList']),
  updateJackpotRef = ref()
  const action = reactive({
    dialogTableVisible:false,
    disabled:false,
    gameName:'',
    white_id:'',
    game_id:'',
    list:[] as Array<any>,
  })
  const handleForm = async()=>{
    updateJackpotRef.value.openDialog({
      white_id:action.white_id,
      game_id:action.game_id,
    })
  }
  const handleDelete = (row:any = {})=>{
    const { id=0, uuid='' } = row
    if(!id) return ToastWarning('缺少id')
    ElMessageBox.confirm(`确认删除ID：${id}白名单的配置吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      ChallengeApi.whiteConfigDelete({id}).then(()=>{
        ToastSuccess("删除成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const getList = async()=>{
    const loading = Toastloading()
    const res = await ChallengeApi.whiteConfigList({
      white_id:action.white_id
    })
    loading.close()
    const { data=[] } = res
    action.list = data
  }
  
  const openDialog = async(query:any = {})=> {
    const { id, game_id, gameName } = query
    if(!id) return ToastWarning('缺少白名单ID')
    action.white_id = id
    action.game_id = game_id
    await getList()
    action.gameName = gameName
    action.dialogTableVisible = true
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogTableVisible"
    width="55%"
    :title="`${action.gameName}-白名单ID:${action.white_id}白名单配置`">
      <div class="xm-headTab">
        <div class="labelBox">
          <div class="labelItem">
            <el-button type="primary" @click="handleForm()">新增</el-button>
          </div>
        </div>
      </div>
      <el-table
        class="xm-main xm-TableCenter"
        :data="action.list"
        border>
        <el-table-column width="120" prop="circulate" label="次数区间" >
          <template #default="scope">
            {{ scope.row.num_st }}-{{ scope.row.num_ed }}
          </template>
        </el-table-column>
        <el-table-column min-width="180" prop="white_user" label="产出人物">
          <template #default="scope">
            <el-tag v-for="item of scope.row.white_reward" :key="item.item_id">id:{{ item.item_id }} {{ item.item_name }} 数量:{{ item.num }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column min-width="120" prop="content" label="描述" />
        <el-table-column width="120" fixed=right label="操作">
          <template #default="scope">
            <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <updateJackpot ref="updateJackpotRef" @update-list="getList" />
  </div>
</template>